$(function(){
  var dictionary={
     init:function(){
    	 var _this=this;
    	 _this.initTreegrid();
    	 _this.initEvent();
     },
     initEvent:function(){
    	 var _this=this;
 		//添加实现
 		$("#add_dictionary").click(function(){
 			var selTree=_this.dictionaryTreeGrid.treegrid('getSelected');
 			if(!selTree){
 				top.$.messager.alert("提示", "请先选择左侧的父级数据字典！", "info"); 
 				return false;
 			}
 			if('0'==selTree.isaddable){
 				top.$.messager.alert("提示", "该字典类型不允许新增子节点!", "info"); 
 				return false;
 			}
 	        top.$.modalDialog({
                _form:"#dictionaryForm", 
                _treegrid:_this.dictionaryTreeGrid,
                _datagrid:_this.dictDataGrid,
 	            title : '添加数据字典',
 	            href:"/sys_dictionary/add",
 	            onLoad:function(){
 	            	var f=top.$.modalDialog.handler.find("#dictionaryForm"); 
 	            	f.form("reset");
 	            	top.$('#level').val(Number(selTree.level)+1); 
 	            	top.$('#parentId').val(selTree.id); 
	 	           	JSmile.KHttp.send({
			       		url:"/sys_dictionary/getMaxDict",
			       		data:{"parentId":selTree.id}
			       	}).done(function(res){
			       		top.$("#sort").textbox("setValue",res.resExtra.sort);
			       		top.$("#dictValue").textbox("setValue",res.resExtra.dictValue);
			       	});
 	            }
 	        }).dialog('open');
 		});
 		//删除实现
 		$("#del_dictionary").click(function(){
 			var selRows=_this.dictDataGrid.datagrid('getSelections');
 			JSmile.KEasy.del('/sys_dictionary/deleteByIds',selRows,function(res){
 				_this.dictDataGrid.datagrid('clearChecked').datagrid('reload');
 			});
 		});
 		$("#edit_dictionary").click(function(){
 			var selRows=_this.dictDataGrid.datagrid('getChecked');
 			JSmile.KEasy.edit(selRows,function(){
 				if(selRows[0].isinit=='1'){
 					top.$.messager.alert("提示", "系统预置项不可修改!", "info");  
 					return false;
 				}
 				if(selRows[0].isenable=='0'){
 					top.$.messager.alert("提示", "禁用的数据字典不可修改!", "info");  
 					return false;
 				}
 	 	        top.$.modalDialog({
 	                _form:"#dictionaryForm", 
 	               _treegrid:_this.dictionaryTreeGrid,
 	                _datagrid:_this.dictDataGrid,	 	        	
 	 	            title : '字典信息修改',
 	 	            href:"/sys_dictionary/edit",
 	 	            onLoad:function(){
 	 	            	var f=top.$.modalDialog.handler.find("#dictionaryForm"); 
 	 	            	f.form("reset");
 	 	            	f.form("load",selRows[0]);
 	 	            }
 	 	    }).dialog('open');
 			});
 		});
 		//搜索实现
 		$("#search_dictionary").click(function(){
 			_this.dictionaryTreeGrid.treegrid('load',$('#dictionarySearchForm').serializeJson());
 		}); 
     },
     initTreegrid:function(){//初始化各种treegrid
    	var _this=this;
    	_this.dictionaryTreeGrid=$('#dictionary_treegrid').treegrid(JSmile.KEasy.initTreegrid({
	    	url:'/sys_dictionary/findAll?level=2',
	    	treeField:"dictName",
	    	onClickCell: function(field,row){
	    		if(row.level==2){
	    			_this.dictDataGrid.datagrid('load',{"parentId":row.id});
	    		}
	    	},
	    	loadFilter:function(data,parentId){
	    		var d={};
	    		d.rows=JSmile.convertTreegrid(data,{textField:"dictName"});
	    		d.total=data.resData.total;
	    		return d;
	    	},
	    	columns:[[
	   		 {field:'id',hidden:true},
			 {field:'dictName',title:'字典值名称',align:'left',width:100}
		]]
	    }));
    	_this.dictDataGrid=$('#dict_datagrid').datagrid(JSmile.KEasy.initDatagrid({
	    	url:'/sys_dictionary/findAllByPage',
	    	queryParams: {"parentId":-1},
	    	toolbar:'#dictionary_toolbar',
	    	columns:[[
		   		 {field:'id',hidden:true},
				 {field:'dictName',title:'字典值名称',align:'center',width:100},
				 {field:'dictValue',title:'字典值',align:'center',width:100},
				 {field:'dictCode',title:'字典编码',align:'center',width:100},
				 {field:'isenable',title:'是否启用',align:'center',width:80,sortable:true,formatter: function(value,row,index){
					 return JSmile.KStatus.initStatus(value);
				 }},
				 {field:'isvisible',title:'是否可见',align:'center',width:80,sortable:true,formatter: function(value,row,index){
					 return JSmile.KStatus.initStatus(value);
				 }},
				 {field:'isinit',title:'是否系统预置',align:'center',width:80,sortable:true,formatter: function(value,row,index){
					 return JSmile.KStatus.initStatus(value);
				 }},
				 {field:'isaddable',title:'是否可新增',align:'center',width:80,sortable:true,formatter: function(value,row,index){
					 return JSmile.KStatus.initStatus(value);
				 }},
				 {field:'remark',title:'备注',align:'center',width:120}
		]]
	    }));
     }
  };
  dictionary.init();
});